/*
 * @Author: along
 * @Date: 2020-02-14
 */
<template>
    <div class="container push">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box-content">
                    <p class="header-box-title">标题搜索:</p>
                    <el-input
                        v-model="head.title"
                        placeholder="请输入要搜索的内容"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-title">内容搜索:</p>
                    <el-input
                        v-model="head.content"
                        placeholder="请输入要搜索的内容"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-title">是否有图片:</p>
                    <el-select
                        v-model="head.has_image"
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in has_image"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-box-content">
                    <p class="header-box-title">发送状态:</p>
                    <el-select
                        v-model="head.status"
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in status"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-box-content">
                    <p class="header-box-title">发送渠道:</p>
                    <el-select
                        v-model="head.target"
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in target"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-box-content">
                    <span class="header-box-title">选择时间:</span>
                    <el-date-picker
                        v-model="head.time"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-box-content">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnCreatePush()"
                    >新建Push</el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="push"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
                fixed="left"
            />
            <el-table-column
                prop="title"
                label="push标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                fixed="left"
                :width="100"
            />
            <el-table-column
                prop="content"
                label="push内容"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                prop="image_url"
                label="图片(安卓)"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="110"
            >
                <template slot-scope="scope">
                    <img
                        v-if="scope.row.image_url !== ''"
                        :src="scope.row.image_url"
                        style="width: 40px;height: 40px"
                    >
                    <p v-if="scope.row.image_url == ''">--</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="发送状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.status == 0">未发送</p>
                    <p v-if="scope.row.status == 1">已发送</p>
                    <p v-if="scope.row.status == 2">已撤回</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="target"
                label="发送渠道"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.target == 1">安卓</p>
                    <p v-if="scope.row.target == 2">ios</p>
                    <p v-if="scope.row.target == 3">安卓+ios</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="send_time"
                label="发送时间"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                :width="150"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ scope.row.send_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="sent"
                label="实际下发数"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                :render-header="renderHeader"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.target == 1">
                        {{ scope.row.statistic.android.sent }}
                    </div>
                    <div v-if="scope.row.target == 2">
                        {{ scope.row.statistic.ios.sent }}
                    </div>
                    <div
                        v-if="scope.row.target == 3"
                    >
                        {{ scope.row.statistic.ios.sent + scope.row.statistic.android.sent }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="feedback"
                label="到达数"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                :render-header="renderHeader2"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.target == 1">
                        {{ scope.row.statistic.android.feedback }}
                    </div>
                    <div v-if="scope.row.target == 2">
                        {{ scope.row.statistic.ios.feedback }}
                    </div>
                    <div
                        v-if="scope.row.target == 3"
                    >
                        {{ scope.row.statistic.ios.feedback + scope.row.statistic.android.feedback }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="displayed"
                label="展示数"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                :render-header="renderHeader3"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.target == 1">
                        {{ scope.row.statistic.android.displayed }}
                    </div>
                    <div v-if="scope.row.target == 2">
                        {{ scope.row.statistic.ios.displayed }}
                    </div>
                    <div
                        v-if="scope.row.target == 3"
                    >
                        {{ scope.row.statistic.ios.displayed + scope.row.statistic.android.displayed }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="clicked"
                label="点击数"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                :render-header="renderHeader4"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.target == 1">
                        {{ scope.row.statistic.android.clicked }}
                    </div>
                    <div v-if="scope.row.target == 2">
                        {{ scope.row.statistic.ios.clicked }}
                    </div>
                    <div
                        v-if="scope.row.target == 3"
                    >
                        {{ scope.row.statistic.ios.clicked + scope.row.statistic.android.clicked }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="clicked"
                label="点击率"
                show-overflow-tooltip
                :width="120"
                :formatter="emptyFormatter"
                :render-header="renderHeader5"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.target == 1">
                        {{ scope.row.statistic.android['clicked'] == 0 || scope.row.statistic.android['feedback'] == 0 ? 0 : (scope.row.statistic.android['clicked'] / scope.row.statistic.android['feedback']).toFixed(2) }}
                    </div>
                    <div v-if="scope.row.target == 2">
                        {{ scope.row.statistic.ios.clicked == 0 || scope.row.statistic.ios.feedback == 0 ? 0 : (scope.row.statistic.ios.clicked / scope.row.statistic.ios.feedback).toFixed(2) }}
                    </div>
                    <div
                        v-if="scope.row.target == 3"
                        class="cursor-font"
                        @click="fnShowDialog(scope.row)"
                    >
                        {{ scope.row | setClick }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="100"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnNextPush(scope.row, false)"
                        :disabled="scope.row.status !== 0"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeletePush(scope.row)"
                    >
                        删除
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        :disabled="scope.row.status !== 1"
                        @click="fnRevokePush(scope.row)"
                    >
                        撤回
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        :disabled="scope.row.status == 0"
                        @click="fnNextPush(scope.row, true)"
                    >
                        再次发送
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 点击率 -->
        <el-dialog
            width="430px"
            custom-class="schedule-dialog"
            :visible.sync="showDilog"
        >
            <el-table
                :data="tableDataClick"
                height="100%"
                style="width: 100%;"
                border
                ref="push"
            >
                <el-table-column
                    prop="android"
                    label="安卓"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    prop="ios"
                    label="ios"
                    show-overflow-tooltip
                    align="center"
                />
            </el-table>
        </el-dialog>
        <!-- /点击率 -->

        <!-- 添加-编辑push -->
        <el-dialog
            width="630px"
            custom-class="schedule-dialog"
            :visible.sync="showPush"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="目标平台"
                    prop="target"
                >
                    <el-radio-group v-model="ruleForm.target">
                        <el-radio :label="3">全部</el-radio>
                        <el-radio :label="1">安卓</el-radio>
                        <el-radio :label="2">ios</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item
                    label="push标题"
                    prop="title"
                >
                    <div style="display:flex">
                        <el-input
                            v-model="ruleForm.title"
                            size="small"
                            maxlength="20"
                        />
                        <p class="el-length">{{ ruleForm.title.length }}/20</p>
                    </div>
                </el-form-item>
                <el-form-item
                    label="push内容"
                    prop="content"
                >
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 3, maxRows: 3}"
                        placeholder="请输入内容"
                        v-model="ruleForm.content"
                        maxlength="50"
                        id="zd-texterar"
                    />
                    <p class="el-length-text">{{ ruleForm.content.length }}/50</p>
                    <p
                        class="cursor-font el-emj"
                        @click=" isEmj = isEmj ? false : true "
                    >emoji表情<i
                        class="el-icon-close"
                        v-if="isEmj"/></p>
                    <div
                        class="emoji"
                        v-if="isEmj"
                    >
                        <p
                            v-for="(item, index) in emoji"
                            :key="index"
                            class="emoji-list"
                            @click="fnAddEmoji(item)"
                        >
                            {{ item }}
                        </p>
                    </div>
                </el-form-item>
                <el-form-item
                    label="图片"
                    style="position: relative"
                    :class="ruleForm.target == 2 ? 'el-no' : '' "
                >
                    <el-upload
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfGoods(e)}"
                        accept="image/*"
                        class="upload-good-image"
                        action="https://jsonplaceholder.typicode.com/posts/">
                        <img
                            v-if="ruleForm.image_url !== ''"
                            :src="ruleForm.image_url"
                            class="avatar">
                        <i
                            v-else
                            class="el-icon-plus avatar-uploader-icon"
                        />
                    </el-upload>
                    <p class="el-img-right">430*230，小于200KB</p>
                    <p>注:带图片的push仅限安卓用户可见，iOS则不可见</p>
                </el-form-item>
                <el-form-item
                    label="发送时间"
                    prop="send_time"
                >
                    <el-date-picker
                        v-model="ruleForm.send_time"
                        type="datetime"
                        placeholder="选择日期时间"
                        size="small"
                    />
                    <p
                        v-if="ruleForm.send_time !== ''"
                        @click=" ruleForm.send_time = '' "
                    >
                        {{ new Date(ruleForm.send_time).getTime() | getDate }}
                        <i class="el-icon-close cursor-font"/>
                    </p>
                </el-form-item>
                <!-- banner跳转 -->
                <el-form-item
                    class="task-form-item"
                    size="small"
                    label="跳转链接:"
                    prop="app_action"
                >
                    <app-action
                        v-model="ruleForm.app_action"
                        ref="appAction"
                        size="small"
                        style="width: 435px"
                        v-if="showPush"
                    />
                </el-form-item>
                <!-- banner跳转 -->
                <!-- banner跳转 -->
                <el-form-item
                    label="测试预览"
                >
                    <div style="display:flex">
                        <el-input
                            v-model="ruleForm.tel"
                            size="small"
                            placeholder="请输入手机号"
                            maxlength="11"
                        />
                        <el-button
                            type="primary"
                            size="small"
                            style="margin-left: 20px;"
                            @click="fnPreviewPush()"
                        >预览</el-button>
                    </div>
                    <p>注:在正式保存前，可以先输入自己的手机号进行该条push的预览</p>
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="primary"
                        style="margin-left: 155px"
                        @click="submitForm('ruleForm')">保存</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!-- /添加-编辑push -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import {getDate} from '@/assets/js/utils';
import AppAction from "@/components/common/app-action";
export default {
    components: {AppAction},
    mixins: [back],
    data () {
        return {
            is_type: true,
            isEmj: false,
            emoji: ['😀','😁','😂','😃','😄','😅','😆','😉','😊','😋','😎','😍','😘','😗','😙','😚','😇','😐','😑','😶','😏','😣','😥','😮','😯','😪','😫','😴','😌','😛','😜','😝','😒','😓','😔','😕','😲','😷','😖','😞','😟','😤','😢','😭','😦','😧','😨','😬','😰','😱','😳','😵','😡','😠','🌹','🍀','🍎','💰','🌙','🍁','🍂','🍃','🌷','💎','🔪','🔫','🏀','⚽','⚡','👄','👍','🔥'],
            ruleForm: {
                target: 3,
                title: '',
                content: '',
                image_url: '',
                send_time: '',
                app_action: {},
                jump_url: '',
                form_name: ''
            },
            peter_name: '',
            setJump: '请选择',
            list: [],
            link: '',
            param: [],
            paramCont: [],
            options: [],
            linkSelectData: [],
            isapp: true,
            rules: {
                target: [
                    { required: true, message: '请填写目标平台,不可以为空', trigger: 'change' }
                ],
                title: [
                    { required: true, message: '请填写push标题,不可以为空', trigger: 'change' }
                ],
                content: [
                    { required: true, message: '请填写push内容,不可以为空', trigger: 'change' }
                ],
                send_time: [
                    { required: true, message: '请选择发送时间,不可以为空', trigger: 'change' }
                ]
            },
            showPush: false,
            tableDataClick: [],
            showDilog: false,
            head: {
                title: '',
                content: '',
                has_image: 99,
                status: 99,
                target: 3,
                time: ''
            },
            has_image: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '是'
            },{
                value: 0,
                label: '否'
            }],
            status: [{
                value: 99,
                label: '全部'
            },{
                value: 0,
                label: '未发送'
            },{
                value: 1,
                label: '已发送'
            },{
                value: 2,
                label: '已撤回'
            }],
            target: [{
                value: 3,
                label: '全部'
            },{
                value: 1,
                label: '安卓'
            },{
                value: 2,
                label: 'ios'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            push_no: '',
            filterData: {
                sort_field: 0,
                sort_type: 1
            },
        };
    },
    filters: {
        setClick (row) {
            const data = row.statistic;

            if (data.android['clicked'] == 0 && data.ios['clicked'] == 0 || data.android['feedback'] == 0 && data.ios['feedback'] == 0) {
                return '0';
            } else {
                const number = Number((data.android['clicked'] / data.android['feedback']).toFixed(2)) + (data.ios['clicked'] / data.ios['feedback']).toFixed(2);

                return isNaN(number) ? 0 : number;
            }
        },
        getDate (val) {
            let y = new Date(val).getFullYear(),
                m = new Date(val).getMonth() + 1,
                d = new Date(val).getDate(),
                h = new Date(val).getHours(),
                s = new Date(val).getMinutes(),
                c = new Date(val).getSeconds();

            m = m < 10 ? '0' + m : m;
            d = d < 10 ? '0' + d : d;
            h = h < 10 ? '0' + h : h;
            s = s < 10 ? '0' + s : s;
            c = c < 10 ? '0' + c : c;

            return y + '-' + m + '-' + d + ' ' + h + ':' + s + ':' + c;
        }
    },
    computed: {},
    watch: {
        showPush (val) {
            if (!val) {
                this.isapp = true;
                this.paramCont = [];
                this.ruleForm = {
                    target: 3,
                    title: '',
                    content: '',
                    image_url: '',
                    send_time: '',
                    app_action: {},
                    jump_url: '',
                    form_name: ''
                };
                this.$refs['ruleForm'].resetFields();
            }
        }
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.getTableData();
        this.getappAcion();
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: 'push列表管理'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/message_push%5Cget_list', {
                page_id: page || this.page.now_page,
                title: this.head.title,
                content: this.head.content,
                has_image: this.head.has_image,
                status: this.head.status,
                target: this.head.target,
                send_start_time: getDate(new Date(this.head.time[0])),
                send_end_time: getDate(new Date(this.head.time[1])),
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 查看点击率
         */
        fnShowDialog (row) {
            this.tableDataClick = [{
                android: row.statistic.android.clicked == 0 || row.statistic.android.feedback == 0 ? 0 : Number((row.statistic.android.clicked / row.statistic.android.feedback).toFixed(2)),
                ios: row.statistic.ios.clicked == 0 || row.statistic.ios.feedback == 0 ? 0 : Number((row.statistic.ios.clicked / row.statistic.ios.feedback).toFixed(2))
            }];
            this.showDilog = true;
        },

        /**
         * 删除push内容
         */
        fnDeletePush (row) {
            this.$confirm('一旦删除则有可能清空该push的历史数据，是否确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/message_push%5Cdelete',{
                    push_no: row.push_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData(1);
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 撤回push
         */
        fnRevokePush (row) {
            this.$confirm('是否确认撤回？一旦撤回，iOS渠道会清空当前存留在用户手机中的所有掌上大学的通知', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/message_push%5Crevoke',{
                    push_no: row.push_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData(1);
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 再次发送
         */
        fnNextPush (row, status) {
            this.is_type = status ? true : false;
            this.push_no = row.push_no;

            this.$post('/student/message_push%5Cget', {
                push_no: row.push_no
            }, resp => {
                if (resp.code == 1) {
                    this.ruleForm = {
                        title: resp.data.title,
                        content: resp.data.content,
                        target: resp.data.target,
                        image_url: resp.data.image_url,
                        send_time: resp.data.send_time * 1000
                    };

                    let app_action = JSON.parse(resp.data.app_action) ? JSON.parse(resp.data.app_action) : {};

                    setTimeout(() => {
                        this.$refs.appAction.setValue(app_action);
                    }, 300);

                    /*if (resp.data.app_action == '') {
                        return;
                    }

                    if(JSON.stringify(JSON.parse(resp.data.app_action).param) == '{}') {
                        this.setUrlData(JSON.parse(resp.data.app_action));
                        this.showPush = !this.showPush;
                        return;
                    }

                    let app_action = JSON.parse(resp.data.app_action);
                    let app = [];

                    this.$post('/student/app_action%5Cget_cascade_list', {}, resp => {
                        if (resp.code == 1) {
                            resp.data.list.map(item => {
                                if (app_action.action == 'link_outer') {
                                    item.target_list.map(itm => {
                                        if (itm.target == app_action.target) {
                                            app.push(
                                                app_action.action,
                                                JSON.stringify({
                                                    target: app_action.target,
                                                    param: [{
                                                        param: itm.param_list[0].param,
                                                        title: itm.param_list[0].title,
                                                        type: itm.param_list[0].form_type
                                                    }]
                                                })
                                            );
                                            this.setApp_action(app,itm.param_list[0].title,app_action.param.url,itm.target);
                                        }
                                    });
                                }

                                if (app_action.action == 'app_page') {
                                    let length = Object.keys(app_action.param).length;

                                    app = [];
                                    app.push(
                                        app_action.action,{
                                            target: app_action.target,
                                            param: []
                                        }
                                    );
                                    item.target_list.map((itm,idx) => {
                                        if (itm.target == app_action.target) {
                                            itm.param_list.map((option,i) => {
                                                app[1].param.push({
                                                    param: option.param,
                                                    title: option.title,
                                                    type: option.form_type
                                                });
                                            });
                                        }
                                    });
                                    setTimeout(()=>{
                                        if (this.isapp) {
                                            this.setSelect_action(app,length,app_action.param);
                                            this.isapp = false;
                                        }
                                    },200);
                                } else {
                                    return;
                                }
                            });
                        }
                    });*/

                    setTimeout(()=>{
                        this.showPush = !this.showPush;
                    },100);
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * param空回填
         */
        setUrlData (url) {
            this.paramCont = [];
            this.ruleForm.app_action = [
                "app_page",
                JSON.stringify({
                    target : url.target,
                    param: []
                })
            ];
            this.link = {
                action :"app_page",
                target : url.target,
                param: {}
            };
        },

        /**
         * select
         */
        setSelect_action (app,length,url) {
            console.log(app, length,url);
            if (length == 1) {
                let list = JSON.parse(JSON.stringify(app));
                let param = JSON.stringify(app[1]);

                this.paramCont = [{
                    type: 'select',
                    title: list[1].param[0]['title']
                }];
                list[1] = param;
                this.ruleForm.app_action = list;
                this.options = [];
                for(let i in url) {
                    this.$post('/student/app_action%5Cget_param_item_list',{
                        action: 'app_page',
                        target: JSON.parse(list[1]).target,
                        param: i
                    },resp => {
                        if (resp.code == 1 ) {
                            this.options.push(
                                resp.data.list,
                            );
                        } else if (resp.code == 0) {
                            this.options = [];
                        }
                    });
                    this.$set(this.ruleForm, 'jump_url', url[i]);
                    this.link = {
                        "action":"app_page",
                        "target":JSON.parse(list[1]).target,
                        "param":{
                            [i]: url[i]
                        }
                    };
                }
            } else if (length == 2) {
                let data = JSON.parse(JSON.stringify(app));
                let params = JSON.stringify(app[1]);
                let current = [];

                app[1] = params;
                this.ruleForm.app_action = app;

                this.paramCont = [];
                this.list = [];

                if (typeof(data[1]['param']) == 'object') {
                    data[1]['param'].map((item,index)=>{
                        this.paramCont.push({
                            type: 'select',
                            title: item.title,
                            param: item.param,
                            target: data[1].target
                        });
                    });
                }

                this.options = [];
                for(let i in url) {
                    this.$post('/student/app_action%5Cget_param_item_list',{
                        action: 'app_page',
                        target: data[1].target,
                        param: i
                    },resp => {
                        if (resp.code == 1 ) {
                            if (i == 'form_no') {
                                this.options.push(
                                    resp.data.list,
                                );
                            }
                            if (i == 'scene_key') {
                                setTimeout(()=>{
                                    this.options.push(
                                        resp.data.list,
                                    );
                                },200);
                            }
                        } else if (resp.code == 0) {
                            this.options = [];
                        }
                    });
                    this.link = {
                        action :"app_page",
                        target : data[1].target,
                        param : url
                    };
                    this.list.push(url[i]);
                }
            }
        },

        /**
         * link_outer
         */
        setApp_action (app,title,url,target) {
            this.ruleForm.app_action = app;
            this.$set(this.ruleForm, 'jump_url', url);
            this.param = [{
                type: 'text',
                title: title
            }];
            this.link = {"action":"link_outer","target":target,"param":{"url":url}};
        },
        // {"action":"app_page","target":"custom_form_custom_scene","param":{"form_no":"fm_5507cab254059","scene_key":"掌大精英可见"}}

        /**
         * 选择emoji
         */
        fnAddEmoji (item) {
            if (this.ruleForm.content.length >= 50) {
                return;
            } else {
                this.getTextIndex('zd-texterar', item);
                // this.ruleForm.content += item;
            }
        },

        /**
         * 添加表情
         */
        getTextIndex (id, insertTxt) {
            this.$nextTick(()=> {
                var elInput = document.getElementById(id);
                var startPos = elInput.selectionStart;
                var endPos = elInput.selectionEnd;

                if (startPos === undefined || endPos === undefined) return;
                var txt = elInput.value;
                var result = txt.substring(0, startPos) + insertTxt + txt.substring(endPos);

                elInput.value = result;
                elInput.focus();
                elInput.selectionStart = startPos + insertTxt.length;
                elInput.selectionEnd = startPos + insertTxt.length;

                this.ruleForm.content = elInput.value;
            });
        },

        /**
         * 上传图片
         */
        fnUploadImageOfGoods(e) {
            this.fnUploadImage(e, (resp) => {
                this.ruleForm.image_url = resp.data.url;
            }, (resp) => {
                console.log(resp.msg);
            });
        },
        fnUploadImage(e, success, error) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();

            pic.append('file', e.file);
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                success: resp => {
                    if (resp.code == 1) {
                        success(resp);
                    } else {
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                        error(resp);
                    }
                }
            });
        },

        /**
         * 预览push
         */
        fnPreviewPush () {
            if (this.ruleForm.title == '') {
                this.$message({
                    type: 'warning',
                    message: '标题不可以为空',
                    center: true
                });
                return;
            }

            if (this.ruleForm.content == '') {
                this.$message({
                    type: 'warning',
                    message: '内容不可以为空',
                    center: true
                });
                return;
            }

            if (this.ruleForm.target == '') {
                this.$message({
                    type: 'warning',
                    message: '目标平台不可以为空',
                    center: true
                });
                return;
            }

            if (this.ruleForm.tel == '') {
                this.$message({
                    type: 'warning',
                    message: '手机号码不可以为空',
                    center: true
                });
                return;
            }

            if (this.ruleForm.tel.length == 11) {
                this.$post('/student/message_push%5Cpreview',{
                    title: this.ruleForm.title,
                    preview_phone: this.ruleForm.tel,
                    content: this.ruleForm.content,
                    image_url: this.ruleForm.image_url,
                    app_action: JSON.stringify(this.ruleForm.app_action),
                    target: this.ruleForm.target
                },resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            } else {
                this.$message({
                    type: 'warning',
                    message: '请输入正确的手机号码',
                    center: true
                });
                return;
            }

        },

        /**
         * 新建push
         */
        fnCreatePush () {
            this.is_type = true;
            this.showPush = !this.showPush;
        },

        /**
         * 保存push
         */
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.is_type) {
                        this.$post('/student/message_push%5Cadd',{
                            title: this.ruleForm.title,
                            content: this.ruleForm.content,
                            image_url: this.ruleForm.image_url,
                            send_time: new Date(this.ruleForm.send_time).getTime() / 1000,
                            app_action: JSON.stringify(this.ruleForm.app_action),
                            target: this.ruleForm.target
                        },resp => {
                            if (resp.code == 1) {
                                this.showPush = !this.showPush;
                                this.getTableData(1);
                                this.$message({
                                    type: 'success',
                                    message: '操作成功',
                                    center: true
                                });
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else {
                        this.$post('/student/message_push%5Cset',{
                            push_no: this.push_no,
                            title: this.ruleForm.title,
                            content: this.ruleForm.content,
                            image_url: this.ruleForm.image_url,
                            send_time: new Date(this.ruleForm.send_time).getTime() / 1000,
                            app_action: JSON.stringify(this.app_action),
                            target: this.ruleForm.target
                        },resp => {
                            if (resp.code == 1) {
                                this.showPush = !this.showPush;
                                this.getTableData(1);
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功',
                                    center: true
                                });
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 清空链接
         */
        fnClearApp () {
            this.ruleForm.app_action = this.ruleForm.jump_url = '';
            this.list = [];
            this.link = '';
        },

        /**
         * app行为数据处理
         */
        getappAcion () {
            let arr = [];
            let param = '';
            let val = '';

            this.$post('/student/app_action%5Cget_cascade_list', {
            }, resp => {
                if (resp.code == 1) {
                    resp.data.list.map((item,index)=>{
                        arr.push({
                            value: item.action,
                            label: item.title,
                            children: []
                        });
                        item.target_list.map((itm, idx) => {
                            val = {
                                target: itm.target,
                                param: []
                            };
                            itm.param_list.map((option,i)=> {
                                val.param.push({
                                    param: option.param,
                                    title: option.title,
                                    type: option.form_type
                                });
                            });
                            arr[index].children.push({
                                value: JSON.stringify(val),
                                label: itm.title
                            });
                        });
                    });
                }
                this.linkSelectData = arr;
            });
        },

        /**
         * app链接数据处理
         */
        handleChange () {
            let app_action = this.ruleForm.app_action;
            let obj = JSON.parse(app_action[1]);
            let link = {
                action: '',
                target: '',
                param: {}
            };
            let param = [];

            this.ruleForm.form_name = '';
            this.$set(this.ruleForm, 'jump_url', '');

            if (app_action[0] == 'app_page') { //内部链接
                let param_list = obj.param;

                this.param = this.paramCont = this.list = [];

                if (param_list.length == 1) {
                    for(let i in param_list) {
                        param.push(
                            param_list[i]
                        );
                    }
                    this.list = [''];
                } else if (param_list.length == 2) {
                    param_list.map(item=>{
                        param.push({
                            type: 'select',
                            param: item.param,
                            title: item.title,
                            target: obj['target']
                        });
                    });
                    this.list = ['',''];
                }

                this.paramCont = param;

                if (this.paramCont.length == 0) {
                    this.link = {
                        action: app_action[0],
                        target: JSON.parse(app_action[1]).target,
                        param: {}
                    };
                    return;
                }

                this.options = [];
                this.paramCont.map((item,index)=>{
                    if (item.type == 'select') {
                        this.$post('/student/app_action%5Cget_param_item_list',{
                            action: 'app_page',
                            target: obj['target'],
                            param: item.param
                        },resp => {
                            if (resp.code == 1 ) {
                                if (this.paramCont.length == 1) {
                                    this.options.push(
                                        resp.data.list
                                    );
                                } else if (this.paramCont.length == 2){ // 设置顺序
                                    if (item.param == 'form_no') {
                                        this.options.push(
                                            resp.data.list
                                        );
                                    }
                                    if (item.param == 'scene_key') {
                                        setTimeout(()=>{
                                            this.options.push(
                                                resp.data.list
                                            );
                                        },200);
                                    }
                                }
                            } else if (resp.code == 0) {
                                this.options = [];
                            }
                        });
                        link.param[[item['param']]] = this.ruleForm.jump_url;
                    }
                    if (item.type == 'text') {
                        link.param[[item['param']]] = this.ruleForm.form_name;
                    }

                    link.action = app_action[0];
                    link.target = obj['target'];
                });
            } else if (app_action[0] == 'link_outer') { //外部链接
                this.paramCont = [];
                this.ifSelect = 'text';
                this.param = obj.param;

                //链接赋值
                link = {
                    action: app_action[0],
                    target: obj['target'],
                    param: {
                        url : this.ruleForm.jump_url
                    }
                };
            } else {
                return;
            }
            this.link = link;
        },

        /**
         * @abstract  单链接text_select处理
         */
        getJump (url) {
            let param = {};

            this.$set(this.ruleForm, 'jump_url', url);
            for(let key in this.link['param']) {
                param[key] = url;
            }

            this.link['param'] = param;
            console.log(JSON.stringify(this.link), 'app_action');
        },

        /**
         * @abstract  双链接select处理
         */
        getLink () {
            let param = {};

            //保证param字段不重复，否则值会被覆盖
            this.paramCont.map((item,index)=>{
                param[item.param] = this.list[index];
            });

            this.link['param'] = param;
            console.log(JSON.stringify(this.link), 'app_action');
        },

        /**
         * 获取路由参数
         */
        fnParamUrl () {
            let query = this.$route.query;
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            let sortFieldMap = {
                send_time: 1,
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = 0;
                this.filterData.sort_type = 0;
            }

            this.getTableData();
        },

        /**
         * 设置表头
         */
        renderHeader (h) {
            return h('span', {}, [
                h('span', {}, '实际下发数'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '指实际推送的push数量' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader2 (h) {
            return h('span', {}, [
                h('span', {}, '到达数'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '指用户手机端实际收到的push数量' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader3 (h) {
            return h('span', {}, [
                h('span', {}, '展示数'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '指在用户手机上展示曝光过的push数量' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader4 (h) {
            return h('span', {}, [
                h('span', {}, '点击数'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '指产生点击的push数量' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader5 (h) {
            return h('span', {}, [
                h('span', {}, '点击率'),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: '点击率=点击数/到达数' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-box-content {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                .header-box-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .curse-add {
        width: 100%;
        margin-left: 0 !important;
        text-align: center;
        padding: 4px 0 !important;
    }
    .el-length {
        white-space: nowrap;
        position: relative;
        left: -40px;
    }
    .el-length-text {
        white-space: nowrap;
        position: relative;
        left: 445px;
        top: -32px;
    }
    .el-emj {
        white-space: nowrap;
        position: absolute;
        left: 415px;
        top: 72px;
    }
    //emoji
    .emoji {
        width: 180px;
        display: flex;
        flex-wrap: wrap;
        max-height: 150px;
        overflow: hidden;
        overflow-y: auto;
        cursor: pointer;
        background: #f5f5f5;
        border: 1px #fffffe solid;
        position: absolute;
        left: 312px;
        top: 119px;
        z-index: 9999;
        .emoji-list {
            width: 30px;
            height: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .el-no {
        pointer-events: none;
    }
    .el-img-right {
        position: absolute;
        left: 130px;
        top: 20px;
    }
    .el-select-title {
        display: inline-block;
        color: #909399;
        background: #F5F7FA;
        padding: 0 18px;
        border: 1px solid #DCDFE6;
        border-radius: 4px 0 0 4px;
        border-right: none;
        height: 32px;
        white-space: nowrap;
    }
</style>
<style lang="less">
    .push .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .push .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .push .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .push .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
    .push .emoji-container {
        left: 108px !important;
        top: 139px !important;
        // display: block !important;
    }
    .push .upload-good-image{
        border: 1px solid red;
        width: 100px;
        height: 100px;
        display: flex;
        border: 1px dashed #d9d9d9;
        border-radius: 4px;
        align-items: center;
        justify-content: center;
        font-size: 34px;
        color: #8c939d;
        img{
            width: 100px;
            height: 100px;
        }
        .upload-hide {
            width: 100px;
            height: 100px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
            display: flex !important;
            align-items: center;
            justify-content: center;
        }
    }
</style>
